﻿using OneOfAll.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace OneOfAll.Common.Bussiness
{
    public sealed class ScreenHelper
    {
        private static ScreenHelper _Instance = new ScreenHelper();
        public static ScreenHelper Instance
        {
            get { return _Instance; }
        }

        public void AddObject(string screenName, BaseObjectModel info)
        {
            info.ScreenName = screenName;
            ScreenModel screen = Global.GlobalResource.ResourceConfig.ScreenList.FirstOrDefault(t => t.ScreenName == screenName);
            if (screen == null)
            {
                screen = new ScreenModel(128, 128) { ScreenName = screenName };
                Global.GlobalResource.ResourceConfig.ScreenList.Add(screen);
            }
            screen.AddObject(info);
        }


        public void DeleteObject(string screenName, BaseObjectModel info)
        {
            info.ScreenName = string.Empty;
            ScreenModel screen = Global.GlobalResource.ResourceConfig.ScreenList.FirstOrDefault(t => t.ScreenName == screenName);
            if (screen != null)
            screen.ObjectList.Remove(info);
        }

        public void GetMonsterStatus(string screenName, MonsterModel info)
        {
            ScreenModel screen = Global.GlobalResource.ResourceConfig.ScreenList.FirstOrDefault(t => t.ScreenName == screenName);
            if (screen == null) return;
            var peopleList = screen.ObjectList.Where(t => t.Type == ObjectTypeEnum.人物).ToList();
            var people = peopleList.OrderBy(t => Math.Abs(t.X - info.X) + Math.Abs(t.Y - info.Y)).FirstOrDefault();
            if (people != null)
            {
                double distance = Math.Abs(people.X - info.X) + Math.Abs(people.Y - info.Y);
                if (distance <= info.AttachRange)
                {
                    info.Status = ObjectStatusEnum.攻击;
                }
                else if (distance > info.AttachRange && distance <= info.ViewRange)
                {
                    info.Status = ObjectStatusEnum.追击;
                }
                else
                {
                    Random random = new Random(Guid.NewGuid().GetHashCode());
                    if (random.Next(100) > 50)
                    {
                        info.Status = ObjectStatusEnum.空闲;
                    }
                    else
                        info.Status = ObjectStatusEnum.巡逻;
                    people = null;
                }
            }
            info.AttachPeople = people as PeopleModel; 
        }

        public void GetPeopleStatus(string screenName, PeopleModel info)
        {
            ScreenModel screen = Global.GlobalResource.ResourceConfig.ScreenList.FirstOrDefault(t => t.ScreenName == screenName);
            if (screen == null) return;
            var monsterList = screen.ObjectList.Where(t => t.Type == ObjectTypeEnum.怪物).ToList();
            var monster = monsterList.OrderBy(t => Math.Abs(t.X - info.X) + Math.Abs(t.Y - info.Y)).FirstOrDefault();
            if (monster != null)
            {
                double distance = Math.Abs(monster.X - info.X) + Math.Abs(monster.Y - info.Y);
                if (distance <= info.AttachRange)
                {
                    info.Status = ObjectStatusEnum.攻击;
                }
                else
                {
                    info.Status = ObjectStatusEnum.空闲;
                }
            }
            info.AttachMonster = monster as MonsterModel;
        }


        public List<Point> GetFollowPath(string screenName, MonsterModel info)
        {
            List<Point> path = new List<Point>();
            ScreenModel screen = Global.GlobalResource.ResourceConfig.ScreenList.FirstOrDefault(t => t.ScreenName == screenName);
            if (screen == null) return null;
            path = BehavoirHelper.Instance.GetMonsterFollowPath(screen, info);
            return path;
        }

        public List<Point> GetPatrolPath(string screenName, MonsterModel info)
        {
            List<Point> path = new List<Point>();
            ScreenModel screen = Global.GlobalResource.ResourceConfig.ScreenList.FirstOrDefault(t => t.ScreenName == screenName);
            if (screen == null) return null;
            path = BehavoirHelper.Instance.GetMonsterPatrolPath(screen, info);
            return path;
        }


    }
}
